package com.tma.mobilechart;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.tma.mobilechart.utilitys.ColorPickerDialog;
import com.tma.mobilechart.utilitys.ColorPickerDialog.OnColorChangedListener;

public class ColumnChartActivity extends Activity implements OnClickListener,
		ColorPickerDialog.OnColorChangedListener {
	private EditText etColumnChartTitle, etColumnChartDescription;
	private RadioButton rbColumnChartWeightSmall, rbColumnChartWeightMedium,
			rbColumnChartWeightLarge;
	private CheckBox chbColumnChartDisplayValue;
	private CheckBox chbColumnChart3d;
	private EditText etColumnChartXLabel, etColumnChartYLabel;
	private TextView tvColumnChartXLabel, tvColumnChartYLabel;
	private Button btnColumnChartView, btnColumnSave, btnColumnCancel;

	private Button btnColumnChartTitleColor, btnColumnChartContentColor,
			btnColumnChartValueColor;

	private LinearLayout linearInputData;
	private LayoutInflater lInflater;
	private int columnTitleColor, columnContentColor, columnValueColor;
	private OnColorChangedListener columnColorChanged;

	private int colorState = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.layout_editer_columnchart);

		// Set default color
		columnTitleColor = 0xFF51A9FF;
		columnContentColor = 0xFF51A9FF;
		columnValueColor = 0xFF51A9FF;

		columnColorChanged = this;

		etColumnChartTitle = (EditText) findViewById(R.id.et_columnchart_title);
		etColumnChartDescription = (EditText) findViewById(R.id.et_columnchart_description);

		rbColumnChartWeightSmall = (RadioButton) findViewById(R.id.rb_columnchart_weight_small);
		rbColumnChartWeightMedium = (RadioButton) findViewById(R.id.rb_columnchart_weight_medium);
		rbColumnChartWeightLarge = (RadioButton) findViewById(R.id.rb_columnchart_weight_large);

		chbColumnChartDisplayValue = (CheckBox) findViewById(R.id.chb_columnchart_displayvalue);
		chbColumnChart3d = (CheckBox) findViewById(R.id.chb_columnchart_3d);

		tvColumnChartXLabel = (TextView) findViewById(R.id.tv_columnchart_group_xlabel);
		tvColumnChartYLabel = (TextView) findViewById(R.id.tv_columnchart_group_ylabel);

		etColumnChartXLabel = (EditText) findViewById(R.id.et_columnchart_xlabel);
		etColumnChartXLabel.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				tvColumnChartXLabel.setText(etColumnChartXLabel.getText()
						.toString());
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}
		});
		etColumnChartYLabel = (EditText) findViewById(R.id.et_columnchart_ylabel);
		etColumnChartYLabel.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				tvColumnChartYLabel.setText(etColumnChartYLabel.getText()
						.toString());
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub

			}
		});
		btnColumnChartView = (Button) findViewById(R.id.button_columnchart_view);
		btnColumnSave = (Button) findViewById(R.id.button_columnchart_save);
		btnColumnCancel = (Button) findViewById(R.id.button_columnchart_cancel);

		/*btnColumnChartTitleColor = (Button) findViewById(R.id.button_columnchart_color_title);
		btnColumnChartContentColor = (Button) findViewById(R.id.button_columnchart_color_content);
		btnColumnChartValueColor = (Button) findViewById(R.id.button_columnchart_color_value);*/

		// Layout Inflater
		linearInputData = (LinearLayout) findViewById(R.id.linear_input_data);
		lInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		// add listener
		btnColumnChartView.setOnClickListener(this);
		btnColumnSave.setOnClickListener(this);
		btnColumnCancel.setOnClickListener(this);

		/*btnColumnChartTitleColor.setOnClickListener(this);
		btnColumnChartContentColor.setOnClickListener(this);
		btnColumnChartValueColor.setOnClickListener(this);*/

	}

	@Override
	public void colorChanged(int color) {
		// colorState = 0 -> Title Color
		// colorState = 1 -> Content Color
		// colorState = 2 -> Value Color

		/*switch (colorState) {
		case 0:
			columnTitleColor = color;
			btnColumnChartTitleColor.setBackgroundColor(columnTitleColor);
			break;
		case 1:
			columnContentColor = color;
			btnColumnChartContentColor.setBackgroundColor(columnContentColor);
			break;
		case 2:
			columnValueColor = color;
			btnColumnChartValueColor.setBackgroundColor(columnValueColor);
			break;

		default:
			break;
		}
*/
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button_columnchart_view:
			// event
			if (rbColumnChartWeightSmall.isChecked() == true) {
				Toast.makeText(getBaseContext(),
						rbColumnChartWeightSmall.getText(), 200).show();
			}

			else if (rbColumnChartWeightMedium.isChecked() == true) {
				Toast.makeText(getBaseContext(),
						rbColumnChartWeightMedium.getText(), 200).show();

			} else if (rbColumnChartWeightLarge.isChecked() == true) {
				Toast.makeText(getBaseContext(),
						rbColumnChartWeightLarge.getText(), 200).show();

			}

			// Display value check box
			if (chbColumnChartDisplayValue.isChecked()) {
				Toast.makeText(getBaseContext(), "Display value", 200).show();
			} else {
				Toast.makeText(getBaseContext(), "None display value", 200)
						.show();
			}

			// Change mode 2d,3d button
			if (chbColumnChart3d.isChecked()) {
				Toast.makeText(getBaseContext(), "3d", 200).show();
			} else {
				Toast.makeText(getBaseContext(), "2d", 200).show();
			}

			// start render
			Intent columnIntent = new Intent(v.getContext(),
					RenderLineActivity.class);
			startActivityForResult(columnIntent, 0);
			break;
		case R.id.button_columnchart_save:
			Toast.makeText(v.getContext(), "Button Save", 200).show();
			break;
		case R.id.button_columnchart_cancel:
			finish();
			break;
/*		case R.id.button_columnchart_color_title:
			colorState = 0;
			new ColorPickerDialog(v.getContext(), columnColorChanged,
					columnTitleColor).show();
			break;
		case R.id.button_columnchart_color_content:
			colorState = 1;
			new ColorPickerDialog(v.getContext(), columnColorChanged,
					columnContentColor).show();
			break;
		case R.id.button_columnchart_color_value:
			colorState = 2;
			new ColorPickerDialog(v.getContext(), columnColorChanged,
					columnValueColor).show();
			break;*/

		default:
			break;
		}
	}

	// Dynamic Items and Groups methods, onClick events are declared in XML
	public void addItem(View v) {
		View myView = lInflater.inflate(R.layout.dynamic_item_columnchart, null);
		linearInputData.addView(myView);
	}

	public void deleteItem(View v) {
		linearInputData.removeView((View) v.getParent());
	}

	public void setBarColor(final View v) {
		int initialColor = Color.parseColor(v.getTag().toString()
				.replace("0x", "#"));
		new ColorPickerDialog(v.getContext(), new OnColorChangedListener() {

			@Override
			public void colorChanged(int color) {
				// TODO Auto-generated method stub
				v.setBackgroundColor(color);
				String tag = "0x" + Integer.toHexString(color);
				v.setTag(tag);
			}
		}, initialColor).show();
	}
}
